package com.kqb.sgadmin.controller.auth;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.kqb.sgadmin.manager.captcha.CaptchaManager;
import com.kqb.sgadmin.model.dto.admin.AdminLoginDTO;
import com.kqb.sgadmin.model.dto.admin.AdminSaveDTO;
import com.kqb.sgadmin.model.vo.AdminVO;
import com.kqb.sgadmin.model.vo.LoginVO;
import com.kqb.sgadmin.service.AdminService;
import com.kqb.sgadmin.service.pipeline.create.context.AdminCreateContext;
import com.kqb.sgcommon.base.exception.BusinessException;
import com.kqb.sgcommon.base.response.Result;
import com.kqb.sgcommon.base.utils.ThrowUtils;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author 杉果树皮
 */
@RestController
@RequestMapping("/public/auth")
public class AuthController {

    @Resource
    private CaptchaManager captchaManager;

    @Resource
    private AdminService adminService;

    @PostMapping("/token")
    public Result<?> token(@RequestBody @Valid AdminLoginDTO adminLoginDTO) {
        Boolean verify = captchaManager.verify(adminLoginDTO.getCaptchaVerifyParam());
        LoginVO loginVO = new LoginVO();
        if (!verify) {
            loginVO.setCaptchaVerifyResult(false);
            return Result.success(loginVO);
        }
        SaTokenInfo saTokenInfo = adminService.login(adminLoginDTO);
        loginVO.setCaptchaVerifyResult(true);
        loginVO.setTokenName(saTokenInfo.getTokenName());
        loginVO.setTokenValue(saTokenInfo.getTokenValue());
        // TODO: 2025/6/17  权限列表
        loginVO.setPermissions(new ArrayList<>());
        return Result.success(loginVO);
    }


}
